package com.quartet.utils;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 阿里短信服务
 */
@Log4j2
@Component
public class AliSmsUtil {

	@Value("${accessKeyId}")
	private String accessKeyId="LTAI5t6QFTph6MRFc5WvgyJa";

	@Value("${accessKeySecret}")
	private String accessKeySecret="EQpT8eMZqSUNmsqRy3d135a6YKpxtR";

	@Value("${signName}")
	private String signName="北京长城导航";

	@Value("${templateCode}")
	private String templateCode="SMS_224135185";

	@SneakyThrows
	public static Client createClient(String accessKeyId, String accessKeySecret){
		Config config = new Config()
			.setAccessKeyId(accessKeyId)
			.setAccessKeySecret(accessKeySecret);
		config.endpoint = "dysmsapi.aliyuncs.com";
		return new Client(config);
	}

	/**
	 * 向指定手机发送信息
	 * 暂时只存在验证码模板（只能发验证码）
	 */
	@SneakyThrows
	public SendSmsResponse sendSms(String phone, String remark){
		Client client = createClient(accessKeyId, accessKeySecret);
		SendSmsRequest sendSmsRequest = new SendSmsRequest()
			.setPhoneNumbers(phone)
			.setSignName(signName)
			.setTemplateCode(templateCode)
			.setTemplateParam("{\"remark\":\""+remark+"\"}");
		return client.sendSms(sendSmsRequest);
	}

//	public static void main(String[] args) {
//		AliSmsUtil aliSmsUtil=new AliSmsUtil();
//		SendSmsResponse sendSmsResponse = aliSmsUtil.sendSms("12345678912", "20211212");
//		System.out.println(sendSmsResponse);
//	}
}



